package v1

import (
	"gin-example/models"
	"gin-example/pkg/e"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"log"
	"net/http"
	"strconv"
)

func GetStationsForUser(c *gin.Context) {
	data := make(map[string]interface{})
	userId := c.Query("userId")
	valid := validation.Validation{}
	valid.Required(userId, "userId").Message("userId不能为空")
	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		tmp, _ := strconv.Atoi(userId)
		code = e.ERROR_NOT_EXIST
		if models.ExistUserByID(tmp) {
			code = e.SUCCESS
			data["stations"] = models.GetStationsForUser(tmp)
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func GetStationSlaves(c *gin.Context) {
	data := make(map[string]interface{})
	id := c.Query("id")
	valid := validation.Validation{}
	valid.Required(id, "id").Message("Station的ID不能为空")
	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		code = e.SUCCESS
		tmp1, _ := strconv.Atoi(id)
		data["slaves"], data["type"] = models.GetStationSlave(tmp1)
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

func AddStation(c *gin.Context) {
	json := make(map[string]string)
	_ = c.BindJSON(&json)

	name := json["name"]
	lng := json["lng"]
	lat := json["lat"]
	ownerId := json["ownerId"]

	valid := validation.Validation{}
	valid.Required(name, "name").Message("储能站名称不能为空")
	valid.Required(lng, "lng").Message("地理位置经度不能为空")
	valid.Required(lat, "lat").Message("地理位置纬度不能为空")
	valid.Required(ownerId, "ownerId").Message("从属用户的ID不能为空")
	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		code = e.SUCCESS
		tmp1, _ := strconv.ParseFloat(lng, 64)
		tmp2, _ := strconv.ParseFloat(lat, 64)
		tmp3, _ := strconv.Atoi(ownerId)
		models.AddStation(name, tmp1, tmp2, tmp3, 0)
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func UpdateStation(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	code := e.INVALID_PARAMS

	if models.ExistStationByID(id) {
		code = e.SUCCESS
		json := make(map[string]interface{})
		_ = c.BindJSON(&json)
		lng := json["lng"]
		lat := json["lat"]
		status := json["status"]
		if lng != nil {
			tmp, _ := strconv.ParseFloat(lng.(string), 64)
			json["lng"] = tmp
		}
		if lat != nil {
			tmp, _ := strconv.ParseFloat(lat.(string), 64)
			json["lat"] = tmp
		}
		if status != nil {
			tmp, _ := strconv.Atoi(status.(string))
			json["status"] = tmp
		}
		models.UpdateStation(id, json)
	} else {
		code = e.ERROR_NOT_EXIST
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func DeleteStation(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	code := e.INVALID_PARAMS

	if models.ExistStationByID(id) {
		if models.CheckDeleteStation(id) {
			models.DeleteStation(id)
			code = e.SUCCESS
		} else {
			code = e.ERROR_CANNOT_DELETE
		}
	} else {
		code = e.ERROR_NOT_EXIST
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})
}